package com.xinqi.modules.sys.dict.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.core.constant.CacheNames;
import com.xinqi.common.core.utils.StringUtils;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.modules.sys.dict.domain.SysDictDataEntity;
import com.xinqi.modules.sys.dict.mapper.SysDictDataMapper;
import com.xinqi.modules.sys.dict.service.ISysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典 业务层处理
 *
 * @author dgyu
 */
@RequiredArgsConstructor
@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService {

    private final SysDictDataMapper dictDataMapper;


    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Cacheable(cacheNames = CacheNames.SYS_DICT, key = "#dictType")
    @Override
    public List<SysDictDataEntity> selectDictDataByType(String dictType) {
        return dictDataMapper.selectDictDataByType(dictType);
    }

    /**
     * 根据字典名称查询字典数据
     *
     * @param dictData 字典类型
     * @return 字典数据集合信息
     */
    @Override
    @DS("slaver")
    public PageInfo<SysDictDataEntity> selectPageDictDataList(SysDictDataEntity dictData) {
        LambdaQueryWrapper<SysDictDataEntity> wrapper = new LambdaQueryWrapper<SysDictDataEntity>()
            .eq(StringUtils.isNotBlank(dictData.getDictType()), SysDictDataEntity::getDictType, dictData.getDictType())
            .like(StringUtils.isNotBlank(dictData.getDictLabel()), SysDictDataEntity::getDictLabel, dictData.getDictLabel())
            .eq(StringUtils.isNotBlank(dictData.getStatus()), SysDictDataEntity::getStatus, dictData.getStatus())
            .orderByAsc(SysDictDataEntity::getDictSort);
        IPage<SysDictDataEntity> page = dictDataMapper.selectPage(Pages.page(new Param().setPageSize(300L)), wrapper);
        return Pages.convert(page);
    }

}
